package com.tedu.weibo.controller;

import com.tedu.weibo.mapper.CommentMapper;
import com.tedu.weibo.pojo.dto.CommentDTO;
import com.tedu.weibo.pojo.dto.UserRegDTO;
import com.tedu.weibo.pojo.entity.Comment;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.vo.CommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/v1/comment")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;


    @PostMapping("/insert")
    public int insert (@RequestBody CommentDTO commentDTO, HttpSession session){
        User user = (User) session.getAttribute("user");
        if(user==null){
            return 2;
        }else {
            Comment comment = new Comment();
            BeanUtils.copyProperties(commentDTO,comment);
            comment.setUserId(user.getId());
            commentMapper.insert(comment);
            return 1;
        }
    }

    @GetMapping("/selectByWeiboId")
    public List<CommentVO> selectByWeiboId(Integer id){
        return commentMapper.selectByWeiboId(id);
    }

}
